package net.slidinglive.utilities.fragment;

import java.util.List;

import net.slidinglive.utilities.R;
import net.slidinglive.utilities.adapter.MessageBackupTypeAdapter;
import net.slidinglive.utilities.adapter.MessageSourceAdapter;
import net.slidinglive.utilities.enumeration.MessageStorage;
import net.slidinglive.utilities.model.Message;
import net.slidinglive.utilities.util.Constants;
import net.slidinglive.utilities.util.MessageHelper;
import android.annotation.SuppressLint;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * @author Sebastian Hoffmann <bo.hoffi@gmail.com>
 */
@SuppressLint("ValidFragment")
public class MessageBackupFragment
        extends AbstractFragment
        implements OnClickListener {

    private Spinner sourceSpinner;
    private Spinner backupTypeSpinner;

    public MessageBackupFragment() {

    }

    public MessageBackupFragment(final int layoutId, final String title) {

        super(layoutId, title);
    }

    /*
     * (non-Javadoc)
     * 
     * @see net.slidinglive.utilities.fragment.AbstractFragment#prepareView()
     */
    @Override
    protected void prepareView() {

        this.sourceSpinner = (Spinner) this.view.findViewById(R.id.message_source);
        this.sourceSpinner.setAdapter(new MessageSourceAdapter(this.getActivity()));

        this.backupTypeSpinner = (Spinner) this.view.findViewById(R.id.message_backup_type);
        this.backupTypeSpinner.setAdapter(new MessageBackupTypeAdapter(this.getActivity()));

        final Button backupButton = (Button) this.view.findViewById(R.id.message_btn_backup);
        backupButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        final List<Message> m =
                MessageHelper.getMessages(((MessageStorage) this.sourceSpinner.getSelectedItem()).getStorageSource(),
                        this.getActivity(), Constants.CONTENT_RESOLVER_PROJECTION_ALL_COLUMNS);

        if (m.size() > 0) {
            Toast.makeText(this.getActivity(), m.get(0).toDebugString(), Toast.LENGTH_SHORT).show();
        }
    }
}